package com.ssm.config;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.util.Properties;

public class MyBatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.ssm.entity");
        //设置驼峰式映射
        Configuration configuration = new Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        sqlSessionFactoryBean.setConfiguration(configuration);
        //设置分页插件
        // 设置分页插件
        PageInterceptor interceptor = new PageInterceptor();
        // 设置属性
        Properties properties = new Properties();
        properties.setProperty("helperDialect","mysql");
        properties.setProperty("supportMethodsArguments","true");
        properties.setProperty("reasonable","true");
        // 将分页属性设置到分页拦截器中
        interceptor.setProperties(properties);

        // 最后将分页拦截器设置到 SqlSessionFactoryBean 插件中
        sqlSessionFactoryBean.setPlugins(interceptor);

        return sqlSessionFactoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.ssm.mapper");
        return mapperScannerConfigurer;
    }
}
